Skip to content

Add PSR-3 compliant exception context when logging exceptions#300

Open
svenmuennich wants to merge 1 commit intomodelcontextprotocol:mainfrom
pickware:fix/psr3-exception-logging
Open

Add PSR-3 compliant exception context when logging exceptions#300
svenmuennich wants to merge 1 commit intomodelcontextprotocol:mainfrom
pickware:fix/psr3-exception-logging

Conversation

@svenmuennich
Copy link
Copy Markdown

This PR consistently adds caught exceptions to the logging context as exception, following the standard defined by PSR-3.

Motivation and Context

Previously almost no caught exceptions where included in the context passed to respective PSR logging calls, making it very hard for downstream tooling to e.g. filter certain errors from reporting.

How Has This Been Tested?

Added unit test coverage for inclusion of exceptions in logging.

Breaking Changes

n/a

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

I decided to include objects of type Throwable in the logging, even though PSR-3 only covers objects of types Exception. My reasoning was based on the following statement in the spec:

Implementors MUST still verify that the 'exception' key is actually an Exception before using it as such, as it MAY contain anything.

@chr-hertel chr-hertel added enhancement Request for a new feature that's not currently supported labels May 8, 2026
Copy link
Copy Markdown
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that impulse @svenmuennich - nice house-keeping. I'd say we could go one step further, WDYT?

Comment on lines 50 to 52
'error' => $e->getMessage(),
'exception' => $e,
'exception_trace' => $e->getTraceAsString(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can skip the others then I'd guess:

Suggested change
'error' => $e->getMessage(),
'exception' => $e,
'exception_trace' => $e->getTraceAsString(),
'exception' => $e,

Comment on lines +90 to 92
'exception' => $e,
'exception_message' => $e->getMessage(),
'exception_trace' => $e->getTraceAsString(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'exception' => $e,
'exception_message' => $e->getMessage(),
'exception_trace' => $e->getTraceAsString(),
'exception' => $e,

$this->logger->info('Session closed', ['session_id' => $this->sessionId]);
} catch (\Throwable $e) {
$this->logger->warning('Failed to close session', ['error' => $e->getMessage()]);
$this->logger->warning('Failed to close session', ['error' => $e->getMessage(), 'exception' => $e]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->logger->warning('Failed to close session', ['error' => $e->getMessage(), 'exception' => $e]);
$this->logger->warning('Failed to close session', ['exception' => $e]);

@chr-hertel chr-hertel added this to the 0.6.0 milestone May 8, 2026
@chr-hertel chr-hertel added needs more work Not ready to be merged yet, needs additional follow-up from the author(s). labels May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Request for a new feature that's not currently supported needs more work Not ready to be merged yet, needs additional follow-up from the author(s).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants